package lib.base.action

import android.content.Context
import android.graphics.drawable.Drawable
import android.net.ConnectivityManager
import android.view.View
import androidx.annotation.DrawableRes
import androidx.annotation.RawRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.NetworkUtils
import lib.base.R
import lib.base.layout.StatusLayout

/**
 *  author: luoXiong
 *  e-mail: 382060748@qq.com
 *  date: 2021/9/2 13:28
 *  version: 1.0
 *  desc:
 */
interface StatusAction {
    /**
     * 获取状态布局
     */
    val statusLayout: StatusLayout

    /**
     * 显示加载中
     */
    fun showLoading(@RawRes id: Int = R.raw.loading) {
        val layout: StatusLayout = statusLayout
        layout.show()
        layout.setAnimResource(id)
        layout.setHint("")
        layout.setOnClickListener(null)
    }

    /**
     * 显示加载完成
     */
    fun showComplete() {
        val layout: StatusLayout = statusLayout
        if (!layout.isShow) {
            return
        }
        layout.hide()
    }

    /**
     * 显示空提示
     */
    fun showEmpty() {
        showLayout(R.drawable.base_status_empty_ic, R.string.status_layout_no_data, null)
    }

    /**
     * 显示错误提示
     */
    fun showError(listener: View.OnClickListener?) {
        // 判断网络是否连接
        if (NetworkUtils.isConnected()) {
            showLayout(R.drawable.base_status_error_ic, R.string.status_layout_error_request, listener)
        } else {
            showLayout(R.drawable.base_status_nerwork_ic, R.string.status_layout_error_network, listener)
        }
    }

    /**
     * 显示自定义提示
     */
    fun showLayout(@DrawableRes drawableId: Int, @StringRes stringId: Int, listener: View.OnClickListener?) {
        val layout: StatusLayout = statusLayout
        val context: Context = layout.context
        showLayout(ContextCompat.getDrawable(context, drawableId), context.getString(stringId), listener)
    }

    fun showLayout(drawable: Drawable?, hint: CharSequence?, listener: View.OnClickListener?) {
        val layout: StatusLayout = statusLayout
        layout.show()
        layout.setIcon(drawable)
        layout.setHint(hint)
        layout.setOnClickListener(listener)
    }
}